<?php
/*
 * 供应商控制器
 */
header("Content-Type: text/html;charset=UTF-8");
class Suppliers extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'pagination' );
	}

	public function index($offset = 0) {
		$user_name=$this->session->userdata('username');
		if($user_name){
			$this->load->model ( 'suppliers_model', 'suppliers_model' );
			$limit = 10;
			$total = $this->suppliers_model->count_nums ();
			$data['total']=$total;
			$data['offset']=$offset;
			$data ['suppliers'] = $this->suppliers_model->list_suppliers ( $limit, $offset );
			$current_total=count($data ['suppliers']);
			$data['current_total']=$offset+$current_total;

			$config ['base_url'] = base_url () . 'index.php/suppliers/index/';
			$config ['total_rows'] = $total;
			$config ['per_page'] = $limit;
			$config ['num_links'] = 4;
			$config ['first_link'] = '首页';
			$config ['last_link'] = '尾页';
			$this->pagination->initialize ( $config );
			$data ['pag_links'] = $this->pagination->create_links ();

			$data ['page_title'] = '供应商 — 昊美科技CRM系统';
			$data ['class11'] = 'cur';
			$this->load->view ( 'suppliers_list', $data );
		}else{
			$this->load->view ( 'login');
		}
	}
	
	/*
	 * 查看某条记录
	*/
	public function find($id = '') {
		$query = $this->db->select('suppliers.*,region.district')->from('suppliers')->join('region', 'suppliers.region_id = region.id')->where('suppliers_id',$id);
		$query = $this->db->get();
		$find_data['find_suppliers'] = $query->result ();
		$this->load->view ( 'suppliers_view', $find_data );
	}
	
	/*
	 * 供应商查询记录
	 */
	public function check($name = '') {
		$this->db->select('suppliers.*,region.district')->from('suppliers')->join('region', 'suppliers.region_id = region.id')->where('name',urldecode($name))->order_by("suppliers.create_time", "desc");
		$query = $this->db->get();
		$data ['suppliers'] = $query->result ();
		$data ['pag_links'] = '';
		$data ['page_title'] = '供应商— 昊美科技CRM系统';
		$data ['class11'] = 'cur';
		$this->load->view ( 'suppliers_list', $data );
	}
	
	/*通过类别查询供应商*/
	public function check_type_suppliers($type_id = ''){
		$type = '';
		switch ($type_id) {
		  case 1:
		     $type = '设备';
		     break;
		  case 2:
		     $type = '开发';
		     break;
		  case 3:
		     $type = '服务';
		     break;
		  default:
        	 $type = '其他';
		}
		$this->db->select('suppliers.*,region.district')->from('suppliers')->join('region', 'suppliers.region_id = region.id')->where('type',$type)->order_by("suppliers.create_time", "desc");
		$query = $this->db->get();
		$data ['suppliers'] = $query->result ();
		$data ['pag_links'] = '';
		$data ['page_title'] = '供应商— 昊美科技CRM系统';
		$data ['class11'] = 'cur';
		$this->load->view ( 'suppliers_list', $data );
	}
	
	/*
	 * 查看某条编辑记录
	*/
	public function update($id = '') {
		$query = $this->db->get_where ( 'suppliers', array ('suppliers_id' => $id ) );
		$update_data['update_suppliers'] = $query->row();
		
		$query = $this->db->get('region');/*获取区域*/
		$update_data['region'] = $query->result();
		
		$this->load->view ( 'suppliers_edit', $update_data );
	}
	
	public function edit(){
		$data_array = $_POST;
		if(!trim($data_array['name']) || !trim($data_array['type']) || !trim($data_array['contacter_one'])){
			echo '数据含有空值,提交失败';
		}else{
			$suppliers_id = $data_array ['suppliers_id'];
			$type_id = $data_array ['type'];
			switch ($type_id) {
			  case 1:
			     $type = '设备';
			     break;
			  case 2:
			     $type = '开发';
			     break;
			  case 3:
			     $type = '服务';
			     break;
			  default:
	        	 $type = '其他';
			}
			$data_array ['type'] = $type;
			$this->db->where('suppliers_id', $suppliers_id);
			$row = $this->db->update('suppliers', $data_array);
			if($row){
				redirect ( 'suppliers/index/' );
			}
		}
	}
	
	/*
	 * 新增客户
	*/
	public function show_suppliers_add(){ /*显示增加页面*/
		$query = $this->db->get('region');/*获取区域*/
		$data['region'] = $query->result();
		
		$this->load->view ( 'suppliers_add',$data);
	}
	
	public function insert(){
		$data_array = $_POST;
		
		if(!trim($data_array['name']) || !trim($data_array['type']) || !trim($data_array['contacter_one'])){
			echo '数据含有空值,提交失败';
		}else{
			date_default_timezone_set ( 'PRC' );
			$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
			$data_array['create_time'] = $now_time;
			
			$type_id = $data_array ['type'];
			switch ($type_id) {
			  case 1:
			     $type = '设备';
			     break;
			  case 2:
			     $type = '开发';
			     break;
			  case 3:
			     $type = '服务';
			     break;
			  default:
	        	 $type = '其他';
			}
			$data_array ['type'] = $type;
			
			@$row = $this->db->insert('suppliers', $data_array); 
			if($row > 0 ){
				redirect ( 'suppliers/index/' );
			}
		}
	}
	
	/*
	 * 删除客户
	 * */
	public function delete($id = ''){
		$row = $this->db->delete('suppliers', array('suppliers_id' => $id)); 
		if($row > 0){
			redirect ( 'suppliers/index/' );
		}else{
			echo '删除出现问题';
		}
	}
	
	/*累计合同详细详细**/
	public function check_outside_contract(){
		$data_array = $_POST;
		$suppliers_id = $data_array['suppliers_id'];
		$sql = "SELECT outs.subject,outs.contract_no,outs.total_amount,outs.order_id,us.user_name FROM outside_contract outs join user us on outs.owner_id = us.id where outs.suppliers_id = $suppliers_id";
		$query = $this->db->query($sql);
		$result = $query->result();
		$count = count($result);
		for($i=0;$i<$count;$i++){
			$order_id = $result[$i]->order_id;
			if($order_id){
				$query = $this->db->query('SELECT subject FROM contract where contract_id = '.$order_id.'');
				$subject_row = $query->row_array();
				$result[$i]->order = $subject_row['subject'];
			}else{
				$result[$i]->order = '';
			}
		}
		$json_string = json_encode($result);
		echo $json_string;
	}
}
